﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Xml;
using System.Text.RegularExpressions;
using CMSModel;

/// <summary>
///YwCatch2 的摘要说明
/// </summary>
public class YwCatch2
{
    string urlReg = "<li><span>[\\s\\S]{10,20}</span><a href=\"http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?[\\S\\s]{0,30}\" title=\"[\\S\\s]{0,50}\" target=\"_blank\">[\\S\\s]{3,30}</a></li>";
    string sourceReg = "<div class=\"Info\">[\\S\\s]*?</div>";
    string startReg = "<div id=\"ContentBody\" class=\"Body\">[\\s\\S]*<div class=\"BodyEnd\">";

    //<div class="Line"></div> 
    public void Import()
    {
        WebClient client = new WebClient();
        string str = client.DownloadString("http://finance.eastmoney.com/news/cywjh.html");
        MatchCollection mc = Regex.Matches(str, urlReg, RegexOptions.None);
        for (int i = 0; i < mc.Count; i++)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                string tt = mc[i].Value;
                doc.LoadXml(tt);
                var root = doc.DocumentElement;
                var title = root.ChildNodes[1].InnerText;
                var url = root.ChildNodes[1].Attributes["href"].Value;
                if (IsCheck(url))
                {
                    continue;
                }
                WebClient client2 = new WebClient();
                string str2 = client2.DownloadString(url);
                Match sourceMatch = Regex.Match(str2, sourceReg);
                string source = string.Empty;
                if (sourceMatch.Success)
                {
                    XmlDocument doc2 = new XmlDocument();
                    doc2.LoadXml(sourceMatch.Value);
                    var doc2Root = doc2.DocumentElement;
                    source = doc2Root.LastChild.InnerText;
                }
                Match contentMatch = Regex.Match(str2, startReg);
                string content = string.Empty;
                if (contentMatch.Success)
                {
                    content = contentMatch.Value;
                    content = Regex.Replace(content, "<div[^>]*>", "");
                    content = Regex.Replace(content, "</div>", "");
                    content = content.Replace("<div class=\"BodyEnd\">", "");
                    content = Regex.Replace(content, @"<a[^>]*>", "");
                    content = Regex.Replace(content, "</a>", "");
                    content = Regex.Replace(content, @"<img[^>]*>", "");
                    content = Regex.Replace(content, @"<iframe[\S\s]*</iframe>", "");
                    content = Regex.Replace(content, "<span[^>]*>", "");
                    content = Regex.Replace(content, "</span>", "");
                    content = NewsImportBLL.ReplaceGP(content);
                    CMSEntities cms = new CMSEntities();
                    News news = new News();
                    news.Source = source;
                    news.step = 0;
                    news.content = content;
                    news.ctime = DateTime.Now;
                    news.title = title;
                    news.keyword = title;
                    News_Type newsType = new News_Type();
                    newsType.NewsTypeId = "0101";
                    newsType.ctime = DateTime.Now;
                    news.News_Type.Add(newsType);
                    cms.News.AddObject(news);
                    cms.SaveChanges();
                    NewsCatch nCatch = new NewsCatch();
                    nCatch.url = url;
                    nCatch.NewsID = news.id;
                    nCatch.CTime = DateTime.Now;
                    cms.NewsCatch.AddObject(nCatch);
                    cms.SaveChanges();
                    new NewsImportBLL().ImportBk(news);
                }
            }
            catch
            {
            }
        }
    }

    bool IsCheck(string url)
    {
        CMSEntities cms = new CMSEntities();
        return cms.NewsCatch.Any(x => x.url == url);
    } 
}